#pragma once
#ifndef __SMOOTHER_HEADER__
#define __SMOOTHER_HEADER__

#include <vector>

//////////////////////////////////////////////////////////////////////////

template <typename T>
class Smoother
{
public:
	Smoother(unsigned int numSamples, const T& zeroValue)
		: _samples(numSamples, zeroValue), _nextFreeSample(0), _zeroValue(zeroValue)
	{
		assert(numSamples > 0);
	}

	T update(const T& newSample)
	{
		_samples[_nextFreeSample++] = newSample;
		if (_nextFreeSample == _samples.size())
			_nextFreeSample = 0;

		T avgVal = _zeroValue;
		for (unsigned int i = 0; i < _samples.size(); ++i)
			avgVal += _samples[i];

		avgVal /= static_cast<float>(_samples.size());

		return avgVal;
	}

private:
	std::vector<T> _samples;
	unsigned int _nextFreeSample;
	T _zeroValue;
};

//////////////////////////////////////////////////////////////////////////

#endif // #ifndef __SMOOTHER_HEADER__